#import "HPBorderView.h"


@implementation HPBorderView


#pragma mark - Synthesize


@synthesize borderType;
@synthesize cornerRadius;
@synthesize borderWidth;
@synthesize dashPattern;
@synthesize spacePattern;
@synthesize borderColor;
@synthesize leftTopPoint;
@synthesize bgColor;
@synthesize selectedBgColor;
@synthesize selectedBorderColor;

#pragma mark - Init


- (id)init
{
    self = [super init];
    if (self)
    {
        [self initialize];
    }
    return self;
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self initialize];
    }
    return self;
}


- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        [self initialize];
    }
    return self;
}


- (void)initialize
{
//    [self drawDashedBorder];
}

- (void)dealloc{
    self.borderColor = nil;
    self.bgColor = nil;
    self.selectedBorderColor = nil;
    self.selectedBgColor = nil;
//    _shapeLayer = nil;
    
    [super dealloc];
}
#pragma mark - Drawing


- (void)drawDashedBorder
{
    if (_shapeLayer) [_shapeLayer removeFromSuperlayer];
    
    //border definitions
//	CGFloat cornerRadius = _cornerRadius;
//	CGFloat borderWidth = _borderWidth;
//	NSInteger dashPattern1 = _dashPattern;
//	NSInteger dashPattern2 = _spacePattern;
//	UIColor *lineColor = _borderColor ? _borderColor : [UIColor blackColor];
    BOOL clipFlag = !CGPointEqualToPoint(self.leftTopPoint, CGPointZero);
    //drawing
	CGRect frame = self.bounds;
    
	_shapeLayer = [CAShapeLayer layer];
    
    //creating a path
	CGMutablePathRef path = CGPathCreateMutable();
    
    //drawing a border around a view
	CGPathMoveToPoint(path, NULL, 0, frame.size.height - self.cornerRadius);
	if (!clipFlag) {
        CGPathAddLineToPoint(path, NULL, 0, cornerRadius);
        CGPathAddArc(path, NULL, cornerRadius, cornerRadius, cornerRadius, M_PI, -M_PI_2, NO);
        CGPathAddLineToPoint(path, NULL, frame.size.width - cornerRadius, 0);
    }else{
        CGPathAddLineToPoint(path, NULL, 0, MAX(self.leftTopPoint.y, cornerRadius));
        CGPathMoveToPoint(path, NULL, MAX(self.leftTopPoint.x, cornerRadius), 0);
        CGPathAddLineToPoint(path, NULL, frame.size.width - MAX(self.leftTopPoint.x, cornerRadius), 0);
    }
	CGPathAddArc(path, NULL, frame.size.width - self.cornerRadius, self.cornerRadius, self.cornerRadius, -M_PI_2, 0, NO);
	CGPathAddLineToPoint(path, NULL, frame.size.width, frame.size.height - self.cornerRadius);
	CGPathAddArc(path, NULL, frame.size.width - self.cornerRadius, frame.size.height - self.cornerRadius, self.cornerRadius, 0, M_PI_2, NO);
	CGPathAddLineToPoint(path, NULL, self.cornerRadius, frame.size.height);
	CGPathAddArc(path, NULL, self.cornerRadius, frame.size.height - self.cornerRadius, self.cornerRadius, M_PI_2, M_PI, NO);
    
    //path is set as the _shapeLayer object's path
	_shapeLayer.path = path;
	CGPathRelease(path);
    
    if (selectedStatus) {
        _shapeLayer.strokeColor = self.selectedBorderColor ? [self.selectedBorderColor CGColor] : [[UIColor blackColor] CGColor];
//        _shapeLayer.backgroundColor = self.selectedBgColor ? [self.selectedBgColor CGColor] : [[UIColor clearColor] CGColor];
        self.backgroundColor = self.selectedBgColor ? self.selectedBgColor : [UIColor clearColor];
    } else {
        _shapeLayer.strokeColor = self.borderColor ? [self.borderColor CGColor] : [[UIColor blackColor] CGColor];
//        _shapeLayer.backgroundColor = self.bgColor ? [self.bgColor CGColor] : [[UIColor clearColor] CGColor];
        self.backgroundColor = self.bgColor ? self.bgColor : [UIColor clearColor];
    }
    
//	_shapeLayer.backgroundColor = [[UIColor clearColor] CGColor];
	_shapeLayer.frame = frame;
	_shapeLayer.masksToBounds = NO;
	[_shapeLayer setValue:[NSNumber numberWithBool:NO] forKey:@"isCircle"];
	_shapeLayer.fillColor = [[UIColor clearColor] CGColor];
//	_shapeLayer.strokeColor = self.borderColor ? [self.borderColor CGColor] : [[UIColor blackColor] CGColor];
	_shapeLayer.lineWidth = self.borderWidth;
	_shapeLayer.lineDashPattern = self.borderType == BorderTypeDashed ? [NSArray arrayWithObjects:[NSNumber numberWithInt:self.dashPattern], [NSNumber numberWithInt:self.spacePattern], nil] : nil;
	_shapeLayer.lineCap = kCALineCapRound;
    
    //_shapeLayer is added as a sublayer of the view
	[self.layer addSublayer:_shapeLayer];
	self.layer.cornerRadius = self.cornerRadius;
}


#pragma mark - Setters

- (void)setHPselected:(NSNumber*)flag{
    selectedStatus = [flag boolValue];
    [self refresh];
}

//- (void)setFrame:(CGRect)frame
//{
//    [super setFrame:frame];
//    
//    [self drawDashedBorder];
//}


//- (void)setBorderType:(BorderType)borderType
//{
//    _borderType = borderType;
    
//    [self drawDashedBorder];
//}


//- (void)setCornerRadius:(CGFloat)cornerRadius
//{
//    _cornerRadius = cornerRadius;
    
//    [self drawDashedBorder];
//}


//- (void)setBorderWidth:(CGFloat)borderWidth
//{
//    _borderWidth = borderWidth;
    
//    [self drawDashedBorder];
//}


//- (void)setDashPattern:(NSUInteger)dashPattern
//{
//    _dashPattern = dashPattern;
    
//    [self drawDashedBorder];
//}


//- (void)setSpacePattern:(NSUInteger)spacePattern
//{
//    _spacePattern = spacePattern;
    
//    [self drawDashedBorder];
//}


//- (void)setBorderColor:(UIColor *)borderColor
//{
//    _borderColor = borderColor;
    
//    [self drawDashedBorder];
//}

- (void)refresh{
    [self drawDashedBorder];
}
#pragma mark -


@end